package com.hahaliu.hahaliu.controller.user;

import com.hahaliu.hahaliu.common.result.Result;
import com.hahaliu.hahaliu.common.utils.MinioUtil;
import com.hahaliu.hahaliu.pojo.dto.VideoUploadDTO;
import com.hahaliu.hahaliu.service.video.VideoUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/user/video")
@Api(tags = "用户端视频上传相关接口")
@Slf4j
public class VideoUploadController {

	@Autowired
	private VideoUploadService videoUploadService;

	@Autowired
	private MinioUtil minioUtil;

	@GetMapping("/ask/chunk")
	@ApiOperation("查询当前视频准备要上传的分片序号")
	public Result<Integer> askChunk(@RequestParam("hash") String hash) {
		log.info("准备要上传的分片序号:{}", hash);
		int counts = videoUploadService.askCurrentChunk(hash);

		return Result.success(counts);
	}

	@PostMapping("/upload/chunk")
	@ApiOperation("上传分片")
	public Result uploadChunk(@RequestParam("chunk") MultipartFile chunk, @RequestParam("hash") String hash, @RequestParam("index") Integer index)  {
			videoUploadService.uploadChunk(chunk, hash, index);
			return Result.success();
	}


	@GetMapping("/upload/cancel")
	@ApiOperation("取消上传")
	public Result cancelUpload(@RequestParam("hash") String hash) {
		videoUploadService.cancelUpload(hash);
		return Result.success();
	}

	@PostMapping("/upload/cover")
	@ApiOperation(value = "上传视频封面")
	public Result<String> uploadVideoCover(@RequestParam("file") MultipartFile file) throws IOException {
		log.info("文件上传:{}", file);

		// 原始文件名
		String originalFilename = file.getOriginalFilename();
		// 截取原始文件名的后缀
		String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
		// 构造新文件名称
		String objectName = UUID.randomUUID().toString() + extension;
		// 文件的请求路径
		String filePath = minioUtil.upload(file, "image/cover", objectName);

		return Result.success(filePath);
	}

	@DeleteMapping("/delete/cover")
	@ApiOperation(value = "删除视频封面")
	public Result deleteVideoCover(@RequestParam("path") String path) throws IOException {
		log.info("文件删除:{}", path);

		// 文件的请求路径
		minioUtil.delete(path, "image/cover");

		return Result.success();
	}

	@PostMapping("/add/video")
	@ApiOperation(value = "添加视频投稿")
	public Result addVideo(@RequestParam("coverUrl") MultipartFile coverUrl,
						   @RequestParam("hash") String hash,
						   @RequestParam("title") String title,
						   @RequestParam("type") Integer type,
						   @RequestParam("auth") Integer auth,
						   @RequestParam("duration") Double duration,
						   @RequestParam("mcId") String mcId,
						   @RequestParam("scId") String scId,
						   @RequestParam("tags") String tags,
						   @RequestParam("description") String description) {
		// 原始文件名
		String originalFilename = coverUrl.getOriginalFilename();
		// 截取原始文件名的后缀
		String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
		// 构造新文件名称
		String objectName = UUID.randomUUID().toString() + extension;
		// 文件的请求路径
		String filePath = minioUtil.upload(coverUrl, "image/cover", objectName);

		VideoUploadDTO videoUploadDTO = new VideoUploadDTO(null, filePath, hash, title, duration, type, auth, mcId, scId, tags, description);

		log.info("视频添加:{}", videoUploadDTO);

		videoUploadService.addVideo(videoUploadDTO);

		return Result.success();
	}
}
